package com.ruoyi.web.controller.material.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.controller.material.entity.request.ItemCategoryAddRequest;
import com.ruoyi.web.controller.material.entity.ylProductCategory;
import com.ruoyi.web.controller.material.mapper.YlProductCategoryMapper;
import com.ruoyi.web.controller.material.service.ItemCategoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 物品分类Service业务层处理
 *
 * @author jzx
 * @date 2025-10-26
 */
@Service
public class ItemCategoryServiceImpl implements ItemCategoryService {

    @Resource
    private YlProductCategoryMapper ylProductCategoryMapper;

    /**
     * 查询物品分类管理列表
     *
     * @return 物品管理
     */
    @Override
    public List<ylProductCategory> getPage() {
        LambdaQueryWrapper<ylProductCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(ylProductCategory::getCreateTime);
        return ylProductCategoryMapper.selectList(wrapper);
    }

    /**
     * 删除物品分类管理
     *
     * @param id 物品分类管理主键
     * @return 结果
     */
    @Override
    public void delete(Long[] id) {
        for (Long dictId : id) {
            ylProductCategoryMapper.deleteById(dictId);
        }
    }

    /**
     * 查询详情
     *
     * @param id 物品分类管理主键
     * @return 物品管理
     */
    @Override
    public ylProductCategory selectById(Long id) {
        return ylProductCategoryMapper.selectById(id);
    }

    /**
     * 新增物品分类
     *
     * @param itemCategoryAddRequest 物资分类添加参数
     */
    @Override
    public void add(ItemCategoryAddRequest itemCategoryAddRequest) {
        if (ylProductCategoryMapper.getCategoryName(itemCategoryAddRequest.getCategoryName(), null)) {
            throw new ServiceException("分类名称已存在");
        }
        ylProductCategory ylProductCategory = new ylProductCategory();
        ylProductCategory.setCategoryName(itemCategoryAddRequest.getCategoryName());
        ylProductCategory.setCategoryLevel(itemCategoryAddRequest.getLevel());
        ylProductCategory.setRemark(itemCategoryAddRequest.getRemark());
        ylProductCategoryMapper.insert(ylProductCategory);
    }

    /**
     * 修改物品分类
     *
     * @param itemCategoryUpdateRequest 物资分类修改参数
     */
    @Override
    public void update(ItemCategoryAddRequest itemCategoryUpdateRequest) {
        if (ylProductCategoryMapper.getCategoryName(itemCategoryUpdateRequest.getCategoryName(), String.valueOf(itemCategoryUpdateRequest.getId()))) {
            throw new ServiceException("分类名称已存在");
        }
        ylProductCategoryMapper.update(
                null, // entity 为 null，表示不靠实体更新
                new LambdaUpdateWrapper<ylProductCategory>()
                        .eq(ylProductCategory::getId, itemCategoryUpdateRequest.getId())
                        .set(ylProductCategory::getCategoryName, itemCategoryUpdateRequest.getCategoryName())
                        .set(ylProductCategory::getCategoryLevel, itemCategoryUpdateRequest.getLevel())
                        .set(ylProductCategory::getRemark, itemCategoryUpdateRequest.getRemark())
        );
    }
}
